package com.tennis.tennisreservation.config;

import com.tennis.tennisreservation.service.CouponService;
import com.tennis.tennisreservation.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * 定时任务配置
 */
@Configuration
@EnableScheduling
public class ScheduleConfig {
    
    private static final Logger logger = LoggerFactory.getLogger(ScheduleConfig.class);
    
    @Autowired
    private CouponService couponService;
    
    @Autowired
    private OrderService orderService;
    
    /**
     * 每天中午12点执行，检查并更新过期红包
     */
    @Scheduled(cron = "0 0 12 * * ?")
    public void checkExpiredCoupons() {
        logger.info("开始检查过期红包...");
        int count = couponService.checkAndUpdateExpiredCoupons();
        logger.info("过期红包检查完成，共更新{}个红包状态", count);
    }
    
    /**
     * 每天中午12点执行，清理过期订单
     * 1. 将过期未支付订单标记为已取消
     * 2. 将过期已支付订单标记为已使用
     */
    @Scheduled(cron = "0 0 12 * * ?")
    public void cleanExpiredOrders() {
        logger.info("开始清理过期订单...");
        int count = orderService.cleanExpiredOrders();
        logger.info("过期订单清理完成，共处理{}个订单", count);
    }
} 